package com.muniuliuma.basecontrol;

import java.util.LinkedList;
import android.app.Activity;

public class ActivityStack {

	private LinkedList<Activity> mActivityStack;
	private static ActivityStack sInstance = new ActivityStack();

	private ActivityStack() {
		mActivityStack = new LinkedList<Activity>();
	}

	public static ActivityStack getInstance() {
		return sInstance;
	}

	public boolean hasActivity(Class<? extends Activity> cls) {
		for (Activity i : mActivityStack) {
			if (i.getClass().equals(cls)){
				return true;
			}
		}
		return false;
	}

	public Activity popActivity() {
		if (mActivityStack.isEmpty()){
			return null;
		}
		return mActivityStack.removeFirst();
	}

	public void popActivity(Activity activity) {
		if (activity != null && mActivityStack.isEmpty()) {
			mActivityStack.remove(activity);
		}
	}

	public void pushActivity(Activity activity) {
		mActivityStack.addFirst(activity);
	}

	public Activity topActivity() {
		return mActivityStack.getFirst();
	}

	public Activity getPreviousActivity() {
		if (mActivityStack.isEmpty() || mActivityStack.size() < 2){
			return null;
		}
		Activity activity = mActivityStack.get(1);
		return activity;
	}
	
	public LinkedList<Activity> getStackOfSreen(){
		return mActivityStack;
	}

	public void printStack() {
		for (Activity i : mActivityStack) {
			System.out.println(i.getClass().toString());
		}
	}
}
